package com.yonyou.framework.library.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.mvp.impl.BasePresenter;
import com.yonyou.framework.library.view.BaseView;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 作者：joseph_l on 15/12/15 14:29
 * 邮箱：ljt@yonyou.com
 */
public abstract class BaseFragment<T extends BasePresenter> extends BaseLazyFragment implements BaseView {

    protected T mPresenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        // 获取 Presenter 泛型实例
        Type type = getClass().getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            Type[] types = ((ParameterizedType) type).getActualTypeArguments();
            Class<T> presenterClass = (Class<T>) types[0];
            try {
                mPresenter = presenterClass.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (mPresenter != null) {
            mPresenter.setView(mContext, this);
        }

        super.onCreate(savedInstanceState);

    }

    public void cancelShowEmpty() {
        toggleShowEmpty(false, -1, "");
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void showError(String msg) {
        toggleShowError(true, msg, null);
    }

    @Override
    public void showException(String msg) {
        toggleShowError(true, msg, null);
    }

    @Override
    public void showNetError() {
        toggleNetworkError(true, null);
    }

    @Override
    public void showLoading(String msg) {
        toggleShowLoading(true, null);
    }

    @Override
    public void hideLoading() {
        toggleShowLoading(false, null);
    }

    @Override
    public void showDialogLoading(String msg) {
        toggleShowDialogLoading(msg);
    }

    @Override
    public void dismissDialogLoading() {
        dismissDialogLoad();
    }

//    @Override
//    public void showBusinessError(ErrorBean error) {
//        if (null != error.getCode() && error.getCode().equals("10000"))
//            toggleshowLogin();
//    }
}

